﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebApp.BaseDataSvc;
using YY.Common;

namespace WebApp
{
    public partial class EditBalanceFrm : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(EditBalanceFrm), this);
        }

        [AjaxPro.AjaxMethod]
        public AjaxResult<TB_Balance> GetBalance(int id)
        {
            AjaxResult<TB_Balance> result = new AjaxResult<TB_Balance>();
            result.Data = null;
            result.Msg = null;
            try
            {
                result.Data = this.BaseDataProxy.GetBalance(id);
            }
            catch (Exception ex)
            {
                result.Data = null;
                result.Msg = ex.Message;
            }
            return result;
        }

        [AjaxPro.AjaxMethod]
        public AjaxResult<int> SaveBalance(string id, string eBDate, string eBType, string eAmount, string eTarget, string eBank, string eDesc, string eMemo)
        {
            AjaxResult<int> result = new AjaxResult<int>();
            result.Data = 0;
            result.Msg = null;
            bool update = !string.IsNullOrEmpty(id);
            try
            {
                if (!this.IsPermitOP(EnumCommon.PermissionOpType.Save, this.GetType().Name))
                {
                    result.Msg = "没有操作权限！";
                    return result;
                }

                TB_Balance balance = new TB_Balance();
                if (update)
                    balance = this.BaseDataProxy.GetBalance(Convert.ToInt32(id));
                WebCommon.EvalControlValueToData(balance, "amount", eAmount);
                WebCommon.EvalControlValueToData(balance, "bank", eBank);
                WebCommon.EvalControlValueToData(balance, "bDate", eBDate);
                WebCommon.EvalControlValueToData(balance, "bType", eBType);
                WebCommon.EvalControlValueToData(balance, "desc", eDesc);
                WebCommon.EvalControlValueToData(balance, "memo", eMemo);
                WebCommon.EvalControlValueToData(balance, "target", eTarget);                

                if (update)
                {
                    this.BaseDataProxy.UpdateBalance(balance);
                    result.Data = balance.id;
                }
                else
                {   
                    int newId = this.BaseDataProxy.AddBalance(balance);
                    result.Data = newId;
                }
            }
            catch (Exception ex)
            {
                result.Data = 0;
                result.Msg = ex.Message;
            }
            return result;
        }

        [AjaxPro.AjaxMethod]
        public AjaxResult<bool> DeleteBalance(int id)
        {
            AjaxResult<bool> result = new AjaxResult<bool>();
            result.Data = false;
            result.Msg = null;
            try
            {
                if (!this.IsPermitOP(EnumCommon.PermissionOpType.Delete, this.GetType().Name))
                {
                    result.Msg = "没有操作权限！";
                    return result;
                }

                result.Data = this.BaseDataProxy.DeleteBalance(id);
            }
            catch (Exception ex)
            {
                result.Data = false;
                result.Msg = ex.Message;
            }
            return result;
        }
    }
}
